package com.yuanfeng.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuanfeng.business.dto.ShopDecorationPageAddDTO;
import com.yuanfeng.business.entity.ShopDecorationEntity;
import com.yuanfeng.business.mapper.ShopDecorationMapper;
import com.yuanfeng.business.service.ShopDecorationService;
import com.yuanfeng.commoms.aspect.UserPermissionsResult;
import com.yuanfeng.commoms.aspect.UserService;
import com.yuanfeng.commoms.config.PermissionsConstant;
import com.yuanfeng.commoms.dto.userms.UserTokenBeanDTO;
import com.yuanfeng.commoms.exception.BizCodeEnume;
import com.yuanfeng.commoms.util.ResponseResult;
import org.springframework.stereotype.Service;

/**
 *
 */
@Service
public class ShopDecorationServiceImpl extends ServiceImpl<ShopDecorationMapper, ShopDecorationEntity>
    implements ShopDecorationService{

    @Override
    public ResponseResult addShopDecorationPage(ShopDecorationPageAddDTO param) {
        // 权限判断
        UserPermissionsResult userPermissionsResult = UserService
                .judgePermissions(PermissionsConstant.SERLLER_SHOP_SETSHOP, param.getToken());

        if (userPermissionsResult.getFlag()) {
            param.setShopId(Integer.valueOf(userPermissionsResult.getShopId()));
            ShopDecorationEntity shopDecoration = this.baseMapper
                    .selectOne(new LambdaQueryWrapper<ShopDecorationEntity>().eq(ShopDecorationEntity::getShopId,userPermissionsResult.getShopId()));

            if (shopDecoration == null) {
                this.baseMapper.insertShopDecorationPage(param);
            } else {
                this.baseMapper.updateShopDecorationPage(param);
            }

        } else {
            return ResponseResult.fail(BizCodeEnume.INSUFFICIENT_PERMISSIONS);
        }
        return ResponseResult.success(BizCodeEnume.OPERATION_SUCCESSFUL);
    }

    @Override
    public ResponseResult queryShopDecorationPage(UserTokenBeanDTO param) {
        // 权限判断
        UserPermissionsResult userPermissionsResult = UserService
                .judgePermissions(PermissionsConstant.SERLLER_SHOP_SETSHOP, param.getToken());

        if (userPermissionsResult.getFlag()) {
            Integer shopId = Integer.valueOf(userPermissionsResult.getShopId());
            ShopDecorationEntity shopDecoration = this.baseMapper
                    .selectOne(new LambdaQueryWrapper<ShopDecorationEntity>().eq(ShopDecorationEntity::getShopId,userPermissionsResult.getShopId()));

            if (null == shopDecoration) {
                ShopDecorationPageAddDTO record = new ShopDecorationPageAddDTO();
                record.setShopId(shopId);
                this.baseMapper.insertShopDecorationPage(record);
                shopDecoration = this.baseMapper
                        .selectOne(new LambdaQueryWrapper<ShopDecorationEntity>().eq(ShopDecorationEntity::getShopId,userPermissionsResult.getShopId()));

            }
            return ResponseResult.success(shopDecoration);
        } else {
            return ResponseResult.fail(BizCodeEnume.INSUFFICIENT_PERMISSIONS);
        }
    }
}




